﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AutoEditUI.UserControls
{
    /// <summary>
    /// relate.xaml 的交互逻辑
    /// </summary>
    public partial class relate : UserControl
    {
        public FrameworkElement gx { get; private set; }
        public UIItem gUIItem { get; private set; }
        public MainWindow gmainWindow { get; private set; }

        public relate()
        {
            InitializeComponent();
        }

        private void ValueLeft_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox sss = sender as TextBox;
            gUIItem.LeftValue = Convert.ToDouble(sss.Text);
        }

        internal void SetItm(MainWindow mainWindow, UIItem mUIItem, FrameworkElement x)
        {
            gx = x;
            gUIItem = mUIItem;
            gmainWindow = mainWindow;
            List<string> xxx = new List<string>();
            xxx.Add("");
            foreach (UIItem item in MainWindow.gItems)
            {
                if (mUIItem.Tag != item.Tag && mUIItem.Tag != item.LeftItemTag && mUIItem.Tag != item.UpItemTag)  {
                    xxx.Add(item.Tag);
                }
            }
            ComboBoxParent.ItemsSource = xxx;
            ComboBoxParent.Text = mUIItem.ParentTag;
            TagLeft.ItemsSource = xxx;
            TagUp.ItemsSource = xxx;
            TagLeft.Text = mUIItem.LeftItemTag;
            TagUp.Text = mUIItem.UpItemTag;
            ValueLeft.Text = mUIItem.LeftValue.ToString();
            ValueUp.Text = mUIItem.UpValue.ToString();
            MESBind.ItemsSource = MESWork.MesCodeDictionary;
            MESBind.DisplayMemberPath = "Key";
            MESBind.SelectedValuePath = "Key";
            MESBind.Text = gUIItem.MESBindStr;
        }

        private void TagLeft_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gUIItem.LeftItemTag = (string)TagLeft.SelectedValue;
        }

        private void TagUp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gUIItem.UpItemTag = (string)TagUp.SelectedValue;
        }

        private void MESBind_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gUIItem.MESBindStr= (string)MESBind.SelectedValue;
        }

        private void SetLeft_Click(object sender, RoutedEventArgs e)
        {
            gUIItem.LeftItemTag = TagLeft.Text;
            if (!string.IsNullOrEmpty(ValueLeft.Text))
                gUIItem.LeftValue = Convert.ToDouble(ValueLeft.Text);
            else {
                gUIItem.LeftValue = 0;
            }
            double ddd = UIInit.GetX(gmainWindow, gUIItem);
            gx.SetValue(Canvas.LeftProperty, ddd);
        }

        private void SetUp_Click(object sender, RoutedEventArgs e)
        {
            gUIItem.UpItemTag = TagUp.Text;
            if (!string.IsNullOrEmpty(ValueUp.Text))
                gUIItem.UpValue = Convert.ToDouble(ValueUp.Text);
            else
            {
                gUIItem.UpValue = 0;
            }
            gx.SetValue(Canvas.TopProperty, UIInit.GetY(gmainWindow, gUIItem));
        }

        private void ValueLeft_DragOver(object sender, DragEventArgs e)
        {
           // gUIItem.LeftItemTag = TagLeft.Text;
        }

        private void ValueUp_DragOver(object sender, DragEventArgs e)
        {
           // gUIItem.UpItemTag = TagUp.Text;
        }

        /// <summary>
        /// 限制只能输入数字
        /// </summary>
        /// <param name="e"></param>

        private void ValueUp_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9]+");
            e.Handled = re.IsMatch(e.Text);
        }

        private void GoLeft_Click(object sender, RoutedEventArgs e)
        {
            if (null == gmainWindow.SelectedUIElement) { return; }
            int types = 1;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) {
                types = 10;
            }
                gmainWindow.SelectedUIElement.SetValue(Canvas.LeftProperty, (double)gmainWindow.SelectedUIElement.GetValue(Canvas.LeftProperty) - types);
        }

        private void GoRight_Click(object sender, RoutedEventArgs e)
        {
            if (null == gmainWindow.SelectedUIElement) { return; }
            int types = 1;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                types = 10;
            }
            gmainWindow.SelectedUIElement.SetValue(Canvas.LeftProperty, (double)gmainWindow.SelectedUIElement.GetValue(Canvas.LeftProperty) + types);
        }

        private void GoTop_Click(object sender, RoutedEventArgs e)
        {
            if (null == gmainWindow.SelectedUIElement) { return; }
            int types = 1;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                types = 10;
            }
            gmainWindow.SelectedUIElement.SetValue(Canvas.TopProperty, (double)gmainWindow.SelectedUIElement.GetValue(Canvas.TopProperty) - types);
        }

        private void GoDown_Click(object sender, RoutedEventArgs e)
        {
            if (null == gmainWindow.SelectedUIElement) { return; }
            int types = 1;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                types = 10;
            }
            gmainWindow.SelectedUIElement.SetValue(Canvas.TopProperty, (double)gmainWindow.SelectedUIElement.GetValue(Canvas.TopProperty) + types);
        }



        private void ComboBoxParent_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            gUIItem.ParentTag = (string)ComboBoxParent.SelectedValue;
            MessageBox.Show("Set Parent OK");
        }
    }
}
